#lang r5rs

  (define (deep-reverse x)
    (define (re x prev)
      (if(null? (cdr x))
         (cons
          (if (pair? (car x))
              (deep-reverse (car x))
              (car x))
          prev)
         (re (cdr x) (cons
                      (if (pair? (car x))
                          (deep-reverse (car x))
                          (car x))
                      prev)))
    )
    (re (cdr x) (cons (if (pair? (car x))
                          (deep-reverse (car x))
                          (car x)) '()))
  )
(display (deep-reverse (list 1 2 3 (list 1 3 (list 5 7)))))
  